using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace VirusCollector
{
    public partial class frmMain : Form
    {
        static Brett.VirusDatabase _virusDatabase = null;
        
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = false;
            
            while (true)
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (!System.IO.File.Exists(ofd.FileName))
                    {
                        try
                        {
                            _virusDatabase = new Brett.VirusDatabase(ofd.FileName);
                            break;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                            ofd.Reset();
                        }
                    }
                    else
                    {
                        try
                        {
                            _virusDatabase = new Brett.VirusDatabase(ofd.FileName);
                            break;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                            ofd.Reset();
                        }
                    }
                }
            }

            ofd.Dispose();

            dataGridView1.Columns.Add("clmnChunkLength", "Chunk Length");
            dataGridView1.Columns.Add("clmnHashes", "Hashes");
            dataGridView1.Columns.Add("clmnName", "Name");

            foreach (Brett.VirusDatabase.Virus virus in _virusDatabase.GetAllViruses())
            {
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewTextBoxCell cellChunkLength = new DataGridViewTextBoxCell();
                cellChunkLength.Value = virus.ChunkLength;
                DataGridViewComboBoxCell cellHashes = new DataGridViewComboBoxCell();
                foreach (string s in virus.Hashes)
                {
                    cellHashes.Items.Add(s);
                }
                DataGridViewTextBoxCell cellName = new DataGridViewTextBoxCell();
                cellName.Value = virus.Name;
                row.Cells.Add(cellChunkLength);
                row.Cells.Add(cellHashes);
                row.Cells.Add(cellName);
                dataGridView1.Rows.Add(row);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string virusName = Microsoft.VisualBasic.Interaction.InputBox("Give a name to the virus", "Virus name", "Trojan.Example.Version", 0, 0);
                _virusDatabase.AddVirus(ofd.FileName, virusName);
            }
            ofd.Dispose();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            _virusDatabase.Close();
        }
    }
}